package com.styytech.dadabao.adapter;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.styytech.dadabao.R;
import com.styytech.dadabao.bean.BankItem;

import java.util.LinkedList;

/**
 * Created by User on 2015/8/27.
 */
public class BankItemAdapter extends BaseAdapter {

    private LinkedList<Object> mList = null;
    private LayoutInflater mInflater = null;
    ViewHolder holder = null;
    private Context mContext;
    private Handler handler;
    public BankItemAdapter(LinkedList<Object> mList, Context context,Handler handler) {
        // TODO Auto-generated constructor stub
        this.mList = mList;
        this.mContext = context;
        this.handler=handler;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int arg0) {
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup arg2) {
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(
                    R.layout.adapter_bank, null);
            holder.iv_select=(ImageButton)convertView.findViewById(R.id.iv_select);
            holder.iv_bank=(ImageView)convertView.findViewById(R.id.iv_bank);
            holder.tv_bank_name=(TextView)convertView.findViewById(R.id.tv_bank_name);
            holder.tv_bank_num=(TextView)convertView.findViewById(R.id.tv_bank_num);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final BankItem bankItem = (BankItem) mList.get(position);
        if (bankItem.isSelect()){
            holder.iv_select.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.ic_radio_selected));
        }else{
            holder.iv_select.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.ic_radio_default));
        }


        holder.tv_bank_name.setText(bankItem.getBankName());
        holder.tv_bank_num.setText(getBankNo(bankItem.getAccountNo()));
        convertView.setOnClickListener(new OnclickListener(bankItem,position));
        holder.iv_select.setOnClickListener(new OnclickListener(bankItem,position));


        return convertView;
    }
     class OnclickListener implements View.OnClickListener{
         BankItem bankItem;
         int position;
         private OnclickListener(BankItem bankItem,int position){
                this.bankItem=bankItem;
             this.position=position;
         }
         @Override
         public void onClick(View view) {
             for (int i=0;i<mList.size();i++){
                 ((BankItem) mList.get(i)).setIsSelect(false);
             }
             bankItem.setIsSelect(true);
             notifyDataSetChanged();
             Message message=new Message();
             message.what=1;
             Bundle bundle=new Bundle();
             bundle.putInt("position",position);
             message.setData(bundle);
             handler.sendMessage(message);
         }
     }
    private class ViewHolder {
        //选中
       private ImageButton iv_select;
        //银行图标
        private ImageView iv_bank;
        //银行名字
        private TextView tv_bank_name;
        //银行卡号
        private TextView tv_bank_num;

    }
    private String getBankNo(String bankno){
        StringBuffer xingxing=new StringBuffer();
        for(int i=0;i<bankno.length()-6-4;i++){
            xingxing.append("*");
        }
      return  bankno.substring(0,5)+xingxing.toString()+bankno.substring(bankno.length()-4);
    }
}
